/*
 * Copyright (C) 2010,2011  Paulo Cesar Pereira de Andrade.
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Authors:
 *	Paulo Cesar Pereira de Andrade
 */

/*
 * Prototypes
 */
#if PROTO
static void
fp_trunc_f_fp(f_t *u, f_t v, f_t *w, eint32_t m);

static void
fp_trunc_fp_f(f_t *u, f_t *v, f_t w, eint32_t m);

static void
fp_trunc_fp_fp(f_t *u, f_t *v, f_t *w, eint32_t m);

static void
dp_trunc_d_dp(d_t *u, d_t v, d_t *w, eint32_t m);

static void
dp_trunc_dp_d(d_t *u, d_t *v, d_t w, eint32_t m);

static void
dp_trunc_dp_dp(d_t *u, d_t *v, d_t *w, eint32_t m);

static void
vo_trunc_i_vo(vo_t *u, l_t v, vo_t *w, eint32_t m);

static void
vo_trunc_vo_i(vo_t *u, vo_t *v, l_t w, eint32_t m);

static void
vo_trunc_d_vo(vo_t *u, d_t v, vo_t *w, eint32_t m);

static void
vo_trunc_vo_d(vo_t *u, vo_t *v, d_t w, eint32_t m);

static void
vo_trunc_z_vo(vo_t *u, empz_t v, vo_t *w, eint32_t m);

static void
vo_trunc_vo_z(vo_t *u, vo_t *v, empz_t w, eint32_t m);

static void
vo_trunc_q_vo(vo_t *u, empq_t v, vo_t *w, eint32_t m);

static void
vo_trunc_vo_q(vo_t *u, vo_t *v, empq_t w, eint32_t m);

static void
vo_trunc_r_vo(vo_t *u, empr_t v, vo_t *w, eint32_t m);

static void
vo_trunc_vo_r(vo_t *u, vo_t *v, empr_t w, eint32_t m);

static void
vo_trunc_vo_vo(vo_t *u, vo_t *v, vo_t *w, eint32_t m);

static void
o_trunc_o_o(o_t *u, o_t *v, o_t *w);
#endif

/*
 * Implementation
 */
#if CODE
void
evector_int_trunc(eint_t a, evector_t *b, etype_t t)
{
    eint32_t	 l;
#if HAVE_SIMD
    eint32_t	 d;
#endif
    evector_t	*v;
    etype_t	 vt;
    v_enter();

    l = b->length;
    v_check(2);
    if (a > 2147483647 || a < -2147483647)
	vt = match(t_int64, t, 0, 0);
    else if (a > 32767 || a < -32767)
	vt = match(t_int32, t, 0, 0);
    else if (a > 127 || a < -127)
	vt = match(t_int16, t, 0, 0);
    else
	vt = match(t_int8, t, 0, 0);
    if (vt != t)
	b = coerce(b, t, vt);
    v = create(b, vt);
    switch (vt) {
#if HAVE_SIMD
	case t_int8:
	    if ((l >>= 4))
		vc_div_c_vc(v->v.obj, a, b->v.obj, l);
	    if ((d = v->length & 0xf))
		cp_div_c_cp((c_t *)v->v.obj + (l << 4),
			    a, (c_t *)b->v.obj + (l << 4), d);
	    break;
	case t_int16:
	    if ((l >>= 3))
		vs_div_s_vs(v->v.obj, a, b->v.obj, l);
	    if ((d = v->length & 0x7))
		sp_div_s_sp((s_t *)v->v.obj + (l << 3),
			    a, (s_t *)b->v.obj + (l << 3), d);
	    break;
	case t_int32:
	    if ((l >>= 2))
		vi_div_i_vi(v->v.obj, a, b->v.obj, l);
	    if ((d = v->length & 0x3))
		ip_div_i_ip((i_t *)v->v.obj + (l << 2),
			    a, (i_t *)b->v.obj + (l << 2), d);
	    break;
	case t_int64:
	    if ((l >>= 1))
		vl_div_l_vl(v->v.obj, a, b->v.obj, l);
	    if ((d = v->length & 0x1))
		lp_div_l_lp((l_t *)v->v.obj + (l << 1),
			    a, (l_t *)b->v.obj + (l << 1), d);
	    break;
#else
	case t_int8:
	    vc_div_c_vc(v->v.obj, a, b->v.obj, l);
	    break;
	case t_int16:
	    vs_div_s_vs(v->v.obj, a, b->v.obj, l);
	    break;
	case t_int32:
	    vi_div_i_vi(v->v.obj, a, b->v.obj, l);
	    break;
	case t_int64:
	    vl_div_l_vl(v->v.obj, a, b->v.obj, l);
	    break;
#endif
	case t_float32:
	    fp_trunc_f_fp(v->v.obj, a, b->v.obj, l);
	    break;
	case t_float64:
	    dp_trunc_d_dp(v->v.obj, a, b->v.obj, l);
	    break;
	default:
	    vo_trunc_i_vo(v->v.obj, a, b->v.obj, l);
	    break;
    }
    v_leave();
}

void
evector_trunc_int(evector_t *a, eint_t b, etype_t t)
{
    eint32_t	 l;
#if HAVE_SIMD
    eint32_t	 d;
#endif
    evector_t	*v;
    etype_t	 vt;
    v_enter();

    l = a->length;
    v_check(2);
    if (b > 2147483647 || b < -2147483647)
	vt = match(t_int64, t, 0, 0);
    else if (b > 32767 || b < -32767)
	vt = match(t_int32, t, 0, 0);
    else if (b > 127 || b < -127)
	vt = match(t_int16, t, 0, 0);
    else
	vt = match(t_int8, t, 0, 0);
    if (vt != t)
	a = coerce(a, t, vt);
    v = create(a, vt);
    switch (vt) {
#if HAVE_SIMD
	case t_int8:
	    if ((l >>= 4))
		vc_div_vc_c(v->v.obj, a->v.obj, b, l);
	    if ((d = v->length & 0xf))
		cp_div_cp_c((c_t *)v->v.obj + (l << 4),
			    (c_t *)a->v.obj + (l << 4), b, d);
	    break;
	case t_int16:
	    if ((l >>= 3))
		vs_div_vs_s(v->v.obj, a->v.obj, b, l);
	    if ((d = v->length & 0x7))
		sp_div_sp_s((s_t *)v->v.obj + (l << 3),
			    (s_t *)a->v.obj + (l << 3), b, d);
	    break;
	case t_int32:
	    if ((l >>= 2))
		vi_div_vi_i(v->v.obj, a->v.obj, b, l);
	    if ((d = v->length & 0x3))
		ip_div_ip_i((i_t *)v->v.obj + (l << 2),
			    (i_t *)a->v.obj + (l << 2), b, d);
	    break;
	case t_int64:
	    if ((l >>= 1))
		vl_div_vl_l(v->v.obj, a->v.obj, b, l);
	    if ((d = v->length & 0x1))
		lp_div_lp_l((l_t *)v->v.obj + (l << 1),
			    (l_t *)a->v.obj + (l << 1), b, d);
	    break;
#else
	case t_int8:
	    vc_div_vc_c(v->v.obj, a->v.obj, b, l);
	    break;
	case t_int16:
	    vs_div_vs_s(v->v.obj, a->v.obj, b, l);
	    break;
	case t_int32:
	    vi_div_vi_i(v->v.obj, a->v.obj, b, l);
	    break;
	case t_int64:
	    vl_div_vl_l(v->v.obj, a->v.obj, b, l);
	    break;
#endif
	case t_float32:
	    fp_trunc_fp_f(v->v.obj, a->v.obj, b, l);
	    break;
	case t_float64:
	    dp_trunc_dp_d(v->v.obj, a->v.obj, b, l);
	    break;
	default:
	    vo_trunc_vo_i(v->v.obj, a->v.obj, b, l);
	    break;
    }
    v_leave();
}

void
evector_float_trunc(efloat_t a, evector_t *b, etype_t t)
{
    eint32_t	 l;
    evector_t	*v;
    etype_t	 vt;
    v_enter();

    l = b->length;
    v_check(2);
    vt = match(t_float, t, 0, 0);
    if (vt != t)
	b = coerce(b, t, vt);
    v = create(b, vt);
    switch (vt) {
	case t_float64:
	    dp_trunc_d_dp(v->v.obj, a, b->v.obj, l);
	    break;
	default:
	    vo_trunc_d_vo(v->v.obj, a, b->v.obj, l);
	    break;
    }
    v_leave();
}

void
evector_trunc_float(evector_t *a, efloat_t b, etype_t t)
{
    eint32_t	 l;
    evector_t	*v;
    etype_t	 vt;
    v_enter();

    l = a->length;
    v_check(2);
    vt = match(t_float, t, 0, 0);
    if (vt != t)
	a = coerce(a, t, vt);
    v = create(a, vt);
    switch (vt) {
	case t_float64:
	    dp_trunc_dp_d(v->v.obj, a->v.obj, b, l);
	    break;
	default:
	    vo_trunc_vo_d(v->v.obj, a->v.obj, b, l);
	    break;
    }
    v_leave();
}

void
evector_mpz_trunc(empz_t a, evector_t *b, etype_t t)
{
    evector_t	*v;
    v_enter();

    v_check(2);
    if (t != t_void)
	b = coerce(b, t, t_void);
    v = create(b, t_void);
    vo_trunc_z_vo(v->v.obj, a, b->v.obj, v->length);
    v_leave();
}

void
evector_trunc_mpz(evector_t *a, empz_t b, etype_t t)
{
    evector_t	*v;
    v_enter();

    v_check(2);
    if (t != t_void)
	a = coerce(a, t, t_void);
    v = create(a, t_void);
    vo_trunc_vo_z(v->v.obj, a->v.obj, b, v->length);
    v_leave();
}

void
evector_mpq_trunc(empq_t a, evector_t *b, etype_t t)
{
    evector_t	*v;
    v_enter();

    v_check(2);
    if (t != t_void)
	b = coerce(b, t, t_void);
    v = create(b, t_void);
    vo_trunc_q_vo(v->v.obj, a, b->v.obj, v->length);
    v_leave();
}

void
evector_trunc_mpq(evector_t *a, empq_t b, etype_t t)
{
    evector_t	*v;
    v_enter();

    v_check(2);
    if (t != t_void)
	a = coerce(a, t, t_void);
    v = create(a, t_void);
    vo_trunc_vo_q(v->v.obj, a->v.obj, b, v->length);
    v_leave();
}

void
evector_mpr_trunc(empr_t a, evector_t *b, etype_t t)
{
    evector_t	*v;
    v_enter();

    v_check(2);
    if (t != t_void)
	b = coerce(b, t, t_void);
    v = create(b, t_void);
    vo_trunc_r_vo(v->v.obj, a, b->v.obj, v->length);
    v_leave();
}

void
evector_trunc_mpr(evector_t *a, empr_t b, etype_t t)
{
    evector_t	*v;
    v_enter();

    v_check(2);
    if (t != t_void)
	a = coerce(a, t, t_void);
    v = create(a, t_void);
    vo_trunc_vo_r(v->v.obj, a->v.obj, b, v->length);
    v_leave();
}

void
evector_trunc(evector_t *a, evector_t *b, etype_t at, etype_t bt)
{
    eint32_t	l;
#if HAVE_SIMD
    eint32_t	d;
#endif
    evop_t	o;
    v_enter();

    setup(&o, a, b, at, bt, 0, 0);
    l = o.r->length;
    switch (o.t) {
#if HAVE_SIMD
	case t_int8:
	    if ((l >>= 4))
		vc_div_vc_vc(o.r->v.obj, o.a->v.obj, o.b->v.obj, l);
	    if ((d = o.r->length & 0xf))
		cp_div_cp_cp((c_t *)o.r->v.obj + (l << 4),
			     (c_t *)o.a->v.obj + (l << 4),
			     (c_t *)o.b->v.obj + (l << 4), d);
	    break;
	case t_int16:
	    if ((l >>= 3))
		vs_div_vs_vs(o.r->v.obj, o.a->v.obj, o.b->v.obj, l);
	    if ((d = o.r->length & 0x7))
		sp_div_sp_sp((s_t *)o.r->v.obj + (l << 3),
			     (s_t *)o.a->v.obj + (l << 3),
			     (s_t *)o.b->v.obj + (l << 3), d);
	    break;
	case t_int32:
	    if ((l >>= 2))
		vi_div_vi_vi(o.r->v.obj, o.a->v.obj, o.b->v.obj, l);
	    if ((d = o.r->length & 0x3))
		ip_div_ip_ip((i_t *)o.r->v.obj + (l << 2),
			     (i_t *)o.a->v.obj + (l << 2),
			     (i_t *)o.b->v.obj + (l << 2), d);
	    break;
	case t_int64:
	    if ((l >>= 1))
		vl_div_vl_vl(o.r->v.obj, o.a->v.obj, o.b->v.obj, l);
	    if ((d = o.r->length & 0x1))
		lp_div_lp_lp((l_t *)o.r->v.obj + (l << 1),
			     (l_t *)o.a->v.obj + (l << 1),
			     (l_t *)o.b->v.obj + (l << 1), d);
	    break;
#else
	case t_int8:
	    vc_div_vc_vc(o.r->v.obj, o.a->v.obj, o.b->v.obj, l);
	    break;
	case t_int16:
	    vs_div_vs_vs(o.r->v.obj, o.a->v.obj, o.b->v.obj, l);
	    break;
	case t_int32:
	    vi_div_vi_vi(o.r->v.obj, o.a->v.obj, o.b->v.obj, l);
	    break;
	case t_int64:
	    vl_div_vl_vl(o.r->v.obj, o.a->v.obj, o.b->v.obj, l);
	    break;
#endif
	case t_float32:
	    fp_trunc_fp_fp(o.r->v.obj, o.a->v.obj, o.b->v.obj, l);
	    break;
	case t_float64:
	    dp_trunc_dp_dp(o.r->v.obj, o.a->v.obj, o.b->v.obj, l);
	    break;
	default:
	    vo_trunc_vo_vo(o.r->v.obj, o.a->v.obj, o.b->v.obj, l);
	    break;
    }
    v_leave();
}

static void
fp_trunc_f_fp(f_t *u, f_t v, f_t *w, eint32_t m)
{
    efloat_t	d;

    while (m--) {
	modf(v / *w++, &d);
	if (!finite(d))
	    eraise_exception(except_floating_point_error);
	*u++ = d;
    }
}

static void
fp_trunc_fp_f(f_t *u, f_t *v, f_t w, eint32_t m)
{
    efloat_t	d;

    while (m--) {
	modf(*v++ / w, &d);
	if (!finite(d))
	    eraise_exception(except_floating_point_error);
	*u++ = d;
    }
}

static void
fp_trunc_fp_fp(f_t *u, f_t *v, f_t *w, eint32_t m)
{
    efloat_t	d;

    while (m--) {
	modf(*v++ / *w++, &d);
	if (!finite(d))
	    eraise_exception(except_floating_point_error);
	*u++ = d;
    }
}

static void
dp_trunc_d_dp(d_t *u, d_t v, d_t *w, eint32_t m)
{
    efloat_t	d;

    while (m--) {
	modf(v / *w++, &d);
	if (!finite(d))
	    eraise_exception(except_floating_point_error);
	*u++ = d;
    }
}

static void
dp_trunc_dp_d(d_t *u, d_t *v, d_t w, eint32_t m)
{
    efloat_t	d;

    while (m--) {
	modf(*v++ / w, &d);
	if (!finite(d))
	    eraise_exception(except_floating_point_error);
	*u++ = d;
    }
}

static void
dp_trunc_dp_dp(d_t *u, d_t *v, d_t *w, eint32_t m)
{
    efloat_t	d;

    while (m--) {
	modf(*v++ / *w++, &d);
	if (!finite(d))
	    eraise_exception(except_floating_point_error);
	*u++ = d;
    }
}

static void
vo_trunc_i_vo(vo_t *u, l_t v, vo_t *w, eint32_t m)
{
    eint_t	i;

    for (; m; u++, w++, m--) {
	switch (eobject_type(*w)) {
	    case t_void:
		eraise_exception(except_floating_point_error);
		break;
	    case t_int:
		set_i(v / get_i(w));
		break;
	    case t_float:
		if ((i = ez_trunc_d(thr_zr, v / get_d(w))) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_mpz:
		if (ez_trunc_i_z(thr_zr, v, get_z(w)))
		    set_z(thr_zr);
		else
		    set_i(empz_get_i(thr_zr));
		break;
	    case t_mpq:
		if ((i = ez_trunc_i_q(thr_zr, v, get_q(w))) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_mpr:
		if ((i = ez_trunc_i_r(thr_zr, v, get_r(w))) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    default:
		eraise_exception(except_not_a_real_number);
	}
    }
}

static void
vo_trunc_vo_i(vo_t *u, vo_t *v, l_t w, eint32_t m)
{
    eint_t	i;

    for (; m; u++, v++, m--) {
	switch (eobject_type(*v)) {
	    case t_void:
		if (w == 0)
		    eraise_exception(except_floating_point_error);
		set_i(0);
		break;
	    case t_int:
		set_i(get_i(v) / w);
		break;
	    case t_float:
		if ((i = ez_trunc_d(thr_zr, get_d(v) / w)) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_mpz:
		if ((i = ez_trunc_z_i(thr_zr, get_z(v), w)) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_mpq:
		if ((i = ez_trunc_q_i(thr_zr, get_q(v), w)) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_mpr:
		if ((i = ez_trunc_r_i(thr_zr, get_r(v), w)) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    default:
		eraise_exception(except_not_a_real_number);
	}
    }
}

static void
vo_trunc_d_vo(vo_t *u, d_t v, vo_t *w, eint32_t m)
{
    eint_t	i;

    for (; m; u++, w++, m--) {
	switch (eobject_type(*w)) {
	    case t_void:
		eraise_exception(except_floating_point_error);
	    case t_int:
		if ((i = ez_trunc_d(thr_zr, v / get_i(w))) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_float:
		if ((i = ez_trunc_d(thr_zr, v / get_d(w))) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_mpz:
		if ((i = ez_trunc_d(thr_zr, v / mpz_get_d(get_z(w)))) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_mpq:
		if ((i = ez_trunc_d(thr_zr, v / mpq_get_d(get_q(w)))) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_mpr:
		if ((i = ez_trunc_d_r(thr_zr, v, get_r(w))) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    default:
		eraise_exception(except_not_a_real_number);
	}
    }
}

static void
vo_trunc_vo_d(vo_t *u, vo_t *v, d_t w, eint32_t m)
{
    eint_t	i;
    for (; m; u++, v++, m--) {
	switch (eobject_type(*v)) {
	    case t_void:
		if ((i = ez_trunc_d(thr_zr, 0.0 / w)) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_int:
		if ((i = ez_trunc_d(thr_zr, get_i(v) / w)) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_float:
		if ((i = ez_trunc_d(thr_zr, get_d(v) / w)) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_mpz:
		if ((i = ez_trunc_d(thr_zr, mpz_get_d(get_z(v)) / w)) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_mpq:
		if ((i = ez_trunc_d(thr_zr, mpq_get_d(get_q(v)) / w)) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_mpr:
		if ((i = ez_trunc_r_d(thr_zr, get_r(v), w)) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    default:
		eraise_exception(except_not_a_real_number);
	}
    }
}

static void
vo_trunc_z_vo(vo_t *u, empz_t v, vo_t *w, eint32_t m)
{
    eint_t	i;
    ebool_t	b;

    if (unlikely((b = v == thr_zr))) {
	++thread_self->esp;
	eget_mpz(v, (thread_self->esp - thread_self->ess) - 1);
	v = thread_self->esp[-1].v.o;
    }
    for (; m; u++, w++, m--) {
	switch (eobject_type(*w)) {
	    case t_void:
		eraise_exception(except_floating_point_error);
		break;
	    case t_int:
		if ((i = ez_trunc_z_i(thr_zr, v, get_i(w))) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_float:
		if ((i = ez_trunc_d(thr_zr, mpz_get_d(v) / get_d(w))) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_mpz:
		if (ez_trunc_z_z(thr_zr, v, get_z(w)))
		    set_i(empz_get_i(thr_zr));
		else
		    set_z(thr_zr);
		break;
	    case t_mpq:
		if ((i = ez_trunc_z_q(thr_zr, v, get_q(w))) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_mpr:
		if ((i = ez_trunc_z_r(thr_zr, v, get_r(w))) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    default:
		eraise_exception(except_not_a_real_number);
	}
    }
    if (b)
	--thread_self->esp;
}

static void
vo_trunc_vo_z(vo_t *u, vo_t *v, empz_t w, eint32_t m)
{
    eint_t	i;
    ebool_t	b;

    if (unlikely((b = w == thr_zr))) {
	++thread_self->esp;
	eget_mpz(w, (thread_self->esp - thread_self->ess) - 1);
	w = thread_self->esp[-1].v.o;
    }
    for (; m; u++, v++, m--) {
	switch (eobject_type(*v)) {
	    case t_void:
		set_i(0);
		break;
	    case t_int:
		if (ez_trunc_i_z(thr_zr, get_i(v), w))
		    set_z(thr_zr);
		else
		    set_i(empz_get_i(thr_zr));
		break;
	    case t_float:
		if ((i = ez_trunc_d(thr_zr, get_d(v) / mpz_get_d(w))) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_mpz:
		if (ez_trunc_z_z(thr_zr, get_z(v), w))
		    set_i(empz_get_i(thr_zr));
		else
		    set_z(thr_zr);
		break;
	    case t_mpq:
		if (ez_trunc_q_z(thr_zr, get_q(v), w))
		    set_i(empz_get_i(thr_zr));
		else
		    set_z(thr_zr);
		break;
	    case t_mpr:
		if (ez_trunc_r_z(thr_zr, get_r(v), w))
		    set_i(empz_get_i(thr_zr));
		else
		    set_z(thr_zr);
		break;
	    default:
		eraise_exception(except_not_a_real_number);
	}
    }
    if (b)
	--thread_self->esp;
}

static void
vo_trunc_q_vo(vo_t *u, empq_t v, vo_t *w, eint32_t m)
{
    eint_t	i;
    ebool_t	b;

    if (unlikely((b = v == thr_qr))) {
	++thread_self->esp;
	eget_mpq(v, (thread_self->esp - thread_self->ess) - 1);
	v = thread_self->esp[-1].v.o;
    }
    for (; m; u++, w++, m--) {
	switch (eobject_type(*w)) {
	    case t_void:
		eraise_exception(except_floating_point_error);
		break;
	    case t_int:
		if ((i = ez_trunc_q_i(thr_zr, v, get_i(w))) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_float:
		if ((i = ez_trunc_d(thr_zr, mpq_get_d(v) / get_d(w))) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_mpz:
		if (ez_trunc_q_z(thr_zr, v, get_z(w)))
		    set_i(empz_get_i(thr_zr));
		else
		    set_z(thr_zr);
		break;
	    case t_mpq:
		if ((i = ez_trunc_q_q(thr_zr, v, get_q(w))) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_mpr:
		if ((i = ez_trunc_q_r(thr_zr, v, get_r(w))) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    default:
		eraise_exception(except_not_a_real_number);
	}
    }
    if (b)
	--thread_self->esp;
}

static void
vo_trunc_vo_q(vo_t *u, vo_t *v, empq_t w, eint32_t m)
{
    eint_t	i;
    ebool_t	b;

    if (unlikely((b = w == thr_qr))) {
	++thread_self->esp;
	eget_mpq(w, (thread_self->esp - thread_self->ess) - 1);
	w = thread_self->esp[-1].v.o;
    }
    for (; m; u++, v++, m--) {
	switch (eobject_type(*v)) {
	    case t_void:
		set_i(0);
		break;
	    case t_int:
		if ((i = ez_trunc_i_q(thr_zr, get_i(v), w)) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_float:
		if ((i = ez_trunc_d(thr_zr, get_d(v) / mpq_get_d(w))) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_mpz:
		if ((i = ez_trunc_z_q(thr_zr, get_z(v), w)) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_mpq:
		if ((i = ez_trunc_q_q(thr_zr, get_q(v), w)) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_mpr:
		if ((i = ez_trunc_r_q(thr_zr, get_r(v), w)) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    default:
		eraise_exception(except_not_a_real_number);
	}
    }
    if (b)
	--thread_self->esp;
}

static void
vo_trunc_r_vo(vo_t *u, empr_t v, vo_t *w, eint32_t m)
{
    eint_t	i;

    for (; m; u++, w++, m--) {
	switch (eobject_type(*w)) {
	    case t_void:
		eraise_exception(except_floating_point_error);
	    case t_int:
		if ((i = ez_trunc_r_i(thr_zr, v, get_i(w))) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_float:
		if ((i = ez_trunc_r_d(thr_zr, v, get_d(w))) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_mpz:
		if (ez_trunc_r_z(thr_zr, v, get_z(w)))
		    set_i(empz_get_i(thr_zr));
		else
		    set_z(thr_zr);
		break;
	    case t_mpq:
		if ((i = ez_trunc_r_q(thr_zr, v, get_q(w))) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_mpr:
		if ((i = ez_trunc_r_r(thr_zr, v, get_r(w))) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    default:
		eraise_exception(except_not_a_real_number);
	}
    }
}

static void
vo_trunc_vo_r(vo_t *u, vo_t *v, empr_t w, eint32_t m)
{
    eint_t	i;

    for (; m; u++, v++, m--) {
	switch (eobject_type(*v)) {
	    case t_void:
		/* FIXME result is zero or exception */
		if ((i = ez_trunc_i_r(thr_zr, 0, w)) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_int:
		if ((i = ez_trunc_i_r(thr_zr, get_i(v), w)) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_float:
		if ((i = ez_trunc_d_r(thr_zr, get_d(v), w)) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_mpz:
		if ((i = ez_trunc_z_r(thr_zr, get_z(v), w)) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_mpq:
		if ((i = ez_trunc_q_r(thr_zr, get_q(v), w)) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_mpr:
		if ((i = ez_trunc_r_r(thr_zr, get_r(v), w)) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    default:
		eraise_exception(except_not_a_real_number);
	}
    }
}

static void
vo_trunc_vo_vo(vo_t *u, vo_t *v, vo_t *w, eint32_t m)
{
    for (; m; m--, u++, v++, w++)
	o_trunc_o_o(u, v, w);
}

static void
o_trunc_o_o(o_t *u, o_t *v, o_t *w)
{
    eint_t	i;

    switch (eobject_type(*v)) {
	case t_void:
	    switch (eobject_type(*w)) {
		case t_void:
		    eraise_exception(except_floating_point_error);
		case t_int:
		    if (get_i(w) == 0)
			eraise_exception(except_floating_point_error);
		    set_i(0);
		    break;
		case t_mpz:	case t_mpq:
		    set_i(0);
		    break;
		case t_float:
		    if ((i = ez_trunc_d(thr_zr, 0.0 / get_d(w))) != MININT)
			set_i(i);
		    else
			set_z(thr_zr);
		    break;
		case t_mpr:
		    /* FIXME result is zero or exception */
		    if ((i = ez_trunc_i_r(thr_zr, 0, get_r(w))) != MININT)
			set_i(i);
		    else
			set_z(thr_zr);
		    break;
		default:
		    eraise_exception(except_not_a_real_number);
	    }
	    break;
	case t_int:
	    switch (eobject_type(*w)) {
		case t_void:
		    eraise_exception(except_floating_point_error);
		case t_int:
		    set_i(get_i(v) / get_i(w));
		    break;
		case t_float:
		    if ((i = ez_trunc_d(thr_zr, get_i(v) / get_d(w))) != MININT)
			set_i(i);
		    else
			set_z(thr_zr);
		    break;
		case t_mpz:
		    if (ez_trunc_i_z(thr_zr, get_i(v), get_z(w)))
			set_z(thr_zr);
		    else
			set_i(empz_get_i(thr_zr));
		    break;
		case t_mpq:
		    if ((i = ez_trunc_i_q(thr_zr, get_i(v),
					  get_q(w))) != MININT)
			set_i(i);
		    else
			set_z(thr_zr);
		    break;
		case t_mpr:
		    if ((i = ez_trunc_i_r(thr_zr, get_i(v),
					  get_r(w))) != MININT)
			set_i(i);
		    else
			set_z(thr_zr);
		    break;
		default:
		    eraise_exception(except_not_a_real_number);
	    }
	    break;
	case t_float:
	    switch (eobject_type(*w)) {
		case t_void:
		    eraise_exception(except_floating_point_error);
		case t_int:
		    if ((i = ez_trunc_d(thr_zr, get_d(v) / get_i(w))) != MININT)
			set_i(i);
		    else
			set_z(thr_zr);
		    break;
		case t_float:
		    if ((i = ez_trunc_d(thr_zr, get_d(v) / get_d(w))) != MININT)
			set_i(i);
		    else
			set_z(thr_zr);
		    break;
		case t_mpz:
		    if ((i = ez_trunc_d(thr_zr, get_d(v) /
					mpz_get_d(get_z(w)))) != MININT)
			set_i(i);
		    else
			set_z(thr_zr);
		    break;
		case t_mpq:
		    if ((i = ez_trunc_d(thr_zr, get_d(v) /
					mpq_get_d(get_q(w)))) != MININT)
			set_i(i);
		    else
			set_z(thr_zr);
		    break;
		case t_mpr:
		    if ((i = ez_trunc_d_r(thr_zr, get_d(v),
					  get_r(w))) != MININT)
			set_i(i);
		    else
			set_z(thr_zr);
		    break;
		default:
		    eraise_exception(except_not_a_real_number);
	    }
	    break;
	case t_mpz:
	    switch (eobject_type(*w)) {
		case t_void:
		    eraise_exception(except_floating_point_error);
		case t_int:
		    if ((i = ez_trunc_z_i(thr_zr, get_z(v),
					  get_i(w))) != MININT)
			set_i(i);
		    else
			set_z(thr_zr);
		    break;
		case t_float:
		    if ((i = ez_trunc_d(thr_zr, mpz_get_d(get_z(v)) /
					get_d(w))) != MININT)
			set_i(i);
		    else
			set_z(thr_zr);
		    break;
		case t_mpz:
		    if (ez_trunc_z_z(thr_zr, get_z(v), get_z(w)))
			set_i(empz_get_i(thr_zr));
		    else
			set_z(thr_zr);
		    break;
		case t_mpq:
		    if ((i = ez_trunc_z_q(thr_zr, get_z(v),
					  get_q(w))) != MININT)
			set_i(i);
		    else
			set_z(thr_zr);
		    break;
		case t_mpr:
		    if ((i = ez_trunc_z_r(thr_zr, get_z(v),
					  get_r(w))) != MININT)
			set_i(i);
		    else
			set_z(thr_zr);
		    break;
		default:
		    eraise_exception(except_not_a_real_number);
	    }
	    break;
	case t_mpq:
	    switch (eobject_type(*w)) {
		case t_void:
		    eraise_exception(except_floating_point_error);
		case t_int:
		    if ((i = ez_trunc_q_i(thr_zr, get_q(v),
					  get_i(w))) != MININT)
			set_i(i);
		    else
			set_z(thr_zr);
		    break;
		case t_float:
		    if ((i = ez_trunc_d(thr_zr, mpq_get_d(get_q(v)) /
					get_d(w))) != MININT)
			set_i(i);
		    else
			set_z(thr_zr);
		    break;
		case t_mpz:
		    if (ez_trunc_q_z(thr_zr, get_q(v), get_z(w)))
			set_i(empz_get_i(thr_zr));
		    else
			set_z(thr_zr);
		    break;
		case t_mpq:
		    if ((i = ez_trunc_q_q(thr_zr, get_q(v),
					  get_q(w))) != MININT)
			set_i(i);
		    else
			set_z(thr_zr);
		    break;
		case t_mpr:
		    if ((i = ez_trunc_q_r(thr_zr, get_q(v),
					  get_r(w))) != MININT)
			set_i(i);
		    else
			set_z(thr_zr);
		    break;
		default:
		    eraise_exception(except_not_a_real_number);
	    }
	    break;
	case t_mpr:
	    switch (eobject_type(*w)) {
		case t_void:
		    eraise_exception(except_floating_point_error);
		case t_int:
		    if ((i = ez_trunc_r_i(thr_zr, get_r(v),
					  get_i(w))) != MININT)
			set_i(i);
		    else
			set_z(thr_zr);
		    break;
		case t_float:
		    if ((i = ez_trunc_r_d(thr_zr, get_r(v),
					  get_d(w))) != MININT)
			set_i(i);
		    else
			set_z(thr_zr);
		    break;
		case t_mpz:
		    if (ez_trunc_r_z(thr_zr, get_r(v), get_z(w)))
			set_i(empz_get_i(thr_zr));
		    else
			set_z(thr_zr);
		    break;
		case t_mpq:
		    if ((i = ez_trunc_r_q(thr_zr, get_r(v),
					  get_q(w))) != MININT)
			set_i(i);
		    else
			set_z(thr_zr);
		    break;
		case t_mpr:
		    if ((i = ez_trunc_r_r(thr_zr, get_r(v),
					  get_r(w))) != MININT)
			set_i(i);
		    else
			set_z(thr_zr);
		    break;
		default:
		    eraise_exception(except_not_a_real_number);
	    }
	    break;
	default:
	    eraise_exception(except_not_a_real_number);
    }
}
#endif
